from django.db import models
from django.utils.translation import gettext_lazy as _



# 心理辅导员模型
class Psychologist(models.Model):
    psychologist_id = models.CharField(verbose_name="工号", max_length=100,unique=True,null=True)
    password = models.CharField(verbose_name="密码", max_length=64,null=True)
    username = models.CharField(verbose_name="姓名", max_length=100)
    # CASCADE：级联操作。如果外键对应的那条记录被删除了，那么子表中所有外键为那个记录的数据都会被删除。
    # PROTECT：受保护。即只要子表中有记录引用了外键的那条记录，那么就不能删除外键的那条记录。如果我们强行删除
    # SET_NULL：设置为空。如果外键的那条数据被删除了，那么子表中所有外键为该条记录的对应字段值会被设置为 NULL，前提是要指定这个字段可以为空，否则也会报错；
    records = models.JSONField(verbose_name="心理辅导过的信息记录", null=True, blank=True)
    phone_number = models.CharField(max_length=11, verbose_name="手机号", null=True, blank=True,unique=True)
    def __str__(self):
        return self.name
# 班级模型
class Classroom(models.Model):
    class_name = models.CharField(verbose_name="班级名称", max_length=100,unique=True)
    class_teacher = models.ForeignKey(Psychologist, on_delete=models.SET_NULL, null=True, related_name='teacher_classrooms', verbose_name="班级老师")
    student_count = models.IntegerField(verbose_name="人数", default=0, null=True, blank=True)
    def __str__(self):
        return self.class_name
# 监护人表
class Parents(models.Model):
    # 如果是父母中的一个，可能需要额外的字段来区分是父亲还是母亲
    parents_id = models.CharField(verbose_name="工号", max_length=20,unique=True,null=True)
    password = models.CharField(verbose_name="密码", max_length=64,null=True)
    username = models.CharField(verbose_name="监护人姓名", max_length=100)
    age = models.PositiveIntegerField(_("年龄"))
    relation_to_student = models.CharField(verbose_name="与学生的关系", max_length=50,
                                           choices=(('FATHER', '父亲'), ('MOTHER', '母亲')))
    phone_number = models.CharField(max_length=11, verbose_name="手机号", null=True, blank=True,unique=True)

    def __str__(self):
        return self.name
# # 学生基本信息模型
class Student(models.Model):
    username = models.CharField(verbose_name="姓名", max_length=100)
    age = models.CharField(verbose_name="年龄", max_length=20)
    password = models.CharField(verbose_name="密码", max_length=64,null=True)
    phone_number = models.CharField(max_length=11, verbose_name="手机号", null=True, blank=True,unique=True)
    gender = models.CharField(verbose_name="性别", max_length=1, choices=(('M', '男'), ('F', '女')))
    student_id = models.CharField(verbose_name="学号", max_length=20, unique=True)
    classroom = models.ForeignKey(Classroom, on_delete=models.SET_NULL, null=True, verbose_name="班级",related_name='class_stu')
    birthday = models.CharField(verbose_name="生日", max_length=100)
    psychological_info = models.JSONField(verbose_name="心理信息", null=True, blank=True)
    emotion_and_behavior = models.JSONField(verbose_name="情绪表情与行为", null=True, blank=True)    #{'2024/6/18': {'行为':"打架",'情绪':'愤怒'}}
    student_relations = models.JSONField(verbose_name="学生关系", null=True, blank=True)# {'曾':"朋友",'李':"盆友",'小美':"追求对象"}
    portrait_url = models.URLField(verbose_name="学生人像", null=True, blank=True)
    head_teacher = models.ForeignKey(Psychologist, on_delete=models.SET_NULL, null=True,
                                     related_name='students_as_head_teacher', verbose_name="班主任或心理辅导员")
    # 这里我们假设监护人是一对多关系，因为一个学生可能有一个或多个监护人
    guardians = models.ManyToManyField(Parents, verbose_name=_("监护人"), related_name='students')
    def __str__(self):
        return self.classroom.class_name
# 学生行为模型
class StudentBehavior(models.Model):
    student = models.ForeignKey(Student, on_delete=models.CASCADE, verbose_name="学生")
    date = models.DateField(verbose_name="日期")
    behavior = models.TextField(verbose_name="行为")


class Police(models.Model):
    police_id = models.CharField(verbose_name="账号", max_length=100,unique=True,null=True)
    password = models.CharField(verbose_name="密码", max_length=64,null=True)
    username = models.CharField(verbose_name="姓名", max_length=100)
    handled_affairs = models.JSONField(verbose_name="处理事务",null=True)
    phone_number = models.CharField(max_length=11, verbose_name="手机号", null=True, blank=True,unique=True)
class Camera(models.Model):
    location = models.CharField(verbose_name="摄像头地点", max_length=255)
    dangerous_behavior_video = models.URLField(verbose_name="摄像头拍摄到的危险行为(视频)", null=True, blank=True)
    camera_id = models.CharField(verbose_name="摄像头id", max_length=100, unique=True, null=True)
    gps_location = models.JSONField(verbose_name="GPS位置信息", null=True, blank=True)

    def __str__(self):
        return self.camera_id
class CameraEvent(models.Model):
    camera = models.ForeignKey(Camera, on_delete=models.CASCADE, verbose_name="摄像头", related_name="events")
    timestamp = models.DateTimeField(auto_now_add=True, verbose_name="时间")
    behavior = models.TextField(verbose_name="行为", null=True, blank=True)
    illegal_items = models.TextField(verbose_name="违规物品", null=True, blank=True)

    def __str__(self):
        return f"事件 - 摄像头 {self.camera.camera_id}"


